//
//  NSString+Utility.m
//  Keruyun
//
//  Created by gang.xu on 13-11-27.
//  Copyright (c) 2013年 shishike.com. All rights reserved.
//

#import "NSString+Utility.h"

@implementation NSString (Utility)

// 检测是否为合法字符串
+ (BOOL)isLegal:(NSString *)str
{
    if (str && [str length] > 0 && ![str isEqualToString:kString_Null] && ![str isEqualToString:kString_Null_IOS]) {
        return YES;
    }
    return NO;
}

// 去除前后空格，空行
+ (NSString *)trim:(NSString *)str
{
    if (str == nil) {
        return @"";
    }
    return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

// 转换字符串，过滤nil为@""
+ (NSString *)transString:(NSString *)str
{
    if (str == nil) {
        return @"";
    }
    return str;
}

// 转换性别 none male female
+ (NSString *)translateSex:(NSString *)sex
{
    if ([sex isEqualToString:@"female"]) {
        return @"女士";
    }
    return @"先生";
}

// 转换排队号
+ (NSString *)translateQueueNumber:(NSInteger)queueNumber
{
    if (queueNumber >= 100) {
        return [NSString stringWithFormat:@"%d", queueNumber];
    }
    else if (queueNumber >= 10) {
        return [NSString stringWithFormat:@"0%d", queueNumber];
    }
    else if (queueNumber >= 0) {
        return [NSString stringWithFormat:@"00%d", queueNumber];
    }
    return [NSString stringWithFormat:@"%d", queueNumber];
}

// 本地日期
- (NSDate *)localDateTime
{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *result = [dateFormatter dateFromString:self];
    [dateFormatter release];
    return result;
}

// 本地日期
- (NSDate *)localDate
{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *result = [dateFormatter dateFromString:self];
    [dateFormatter release];
    return result;
}

// 判断是否为IP地址
- (BOOL)isIpAddress
{
    NSArray *strArray = [self componentsSeparatedByString:@"."];
    NSInteger count = [strArray count];
    if (count != 4) {
        return NO;
    }
    for (NSInteger index = 0; index < count; index++) {
        NSString *ipPart = [strArray objectAtIndex:index];
        if ([ipPart length] > 3) {
            return NO;
        }
        for (NSInteger k = 0; k < [ipPart length]; k++) {
            unichar ch = [ipPart characterAtIndex:k];
            if (!(ch >= '0' && ch <= '9')) {
                return NO;
            }
        }
    }
    return YES;
}

// 判断是否为手机号码
- (BOOL)checkMobile
{
    if ([self length] == 0) {
        return NO;
    }
    NSString *regex = @"^((13[0-9])|(147)|(15[0-9])|(18[0-9]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:self];
    if (!isMatch) {
        return NO;
    }
    return YES;
}

// 判断是否为座机号码
- (BOOL)checkTel
{
    if ([self length] == 0) {
        return NO;
    }
    NSString *regex = @"^(([1-9]\\d{6,7})|((0[1-9])\\d{8,10}))$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:self];
    if (!isMatch) {
        return NO;
    }
    return YES;
}

//秒转日期
- (NSDate *)dateFromMilliseconds
{
    return [NSDate dateWithTimeIntervalSince1970:[self doubleValue]/1000.0];
}

// 宽度
- (NSInteger)widthWithFont:(UIFont *)font
{
    CGSize size = [self sizeWithAttributes:@{NSFontAttributeName:font}];
    return size.width + 2;
}

// 根据姓名和性别生成顾客姓名
+ (NSString *)createName:(NSString *)name withSex:(NSString *)sex placeholder:(NSString *)placeholder
{
    if ([NSString isLegal:name]) {
        if ([name length] == 1) {
            if ([sex isEqualToString:@"male"]) {
                return [NSString stringWithFormat:@"%@先生", name];
            }
            else if ([sex isEqualToString:@"female"]) {
                return [NSString stringWithFormat:@"%@女士", name];
            }
            else {
                return name;
            }
        }
        else {
            return name;
        }
    }
    return placeholder;
}

@end
